package com.kitty.record.kittyjava.juc;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description
 * @Author SHEN
 * @Date 2021/1/13
 */
public class ThreadListDemo {

    public static void main(String[] args) {
        //多线程下会发生java.util.ConcurrentModificationException（需要多试几次）
        //List<String> list = new ArrayList();

        //List<String> list = new Vector<>();

        //List<String> list = Collections.synchronizedList(new ArrayList<>());

        //推荐使用juc下面的并发list，性能都优于前面几种方式，具体原因需要自行分析源码
        List<String> list = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 6));
                System.out.println(list);
            }).start();
        }
    }
}
